package com.mingyuan.library.provider.common.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * @author yangzk
 * @version 1.0.0
 * @date 2022.11.06 11:23
 */
@ApiModel(description = "响应结果")
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 7179829822721522947L;
    /**
     * 响应结果
     */
    @ApiModelProperty(name = "success", value = "响应结果")
    private boolean success;
    /**
     * 响应码
     */
    @ApiModelProperty(name = "code", value = "响应码")
    private String code;
    /**
     * 响应描述
     */
    @ApiModelProperty(name = "message", value = "响应描述")
    private String message;
    /**
     * 响应信息
     */
    @ApiModelProperty(name = "message", value = "响应信息")
    private T data;

    public static <T> Result<T> create() {
        return new Result<T>();
    }

    public static <T> Result<T> success() {
        return new Result<T>().setSuccess(true);
    }


//    @SuppressWarnings("unchecked")
//    public static <T> Result<T> success(String code) {
//        return (Result<T>) success().setCode(code);
//    }

    @SuppressWarnings("unchecked")
    public static <T> Result<T> success(String code, String msg) {
        return (Result<T>) success(code).setMessage(msg);
    }

    @SuppressWarnings("unchecked")
    public static <T> Result<T> success(String code, String msg, T data) {
        return (Result<T>) success(code, msg).setData(data);
    }

    @SuppressWarnings("unchecked")
    public static <T> Result<T> success(T data) {
        return (Result<T>) success().setData(data);
    }

    public static <T> Result<T> fail() {
        return new Result<T>().setSuccess(false);
    }

    @SuppressWarnings("unchecked")
    public static <T> Result<T> fail(String code) {
        return (Result<T>) fail().setCode(code);
    }

    @SuppressWarnings("unchecked")
    public static <T> Result<T> fail(String code, String msg) {
        return (Result<T>) fail(code).setMessage(msg);
    }

    public Boolean isSuccess() {
        return success;
    }

    public Result<T> setSuccess(Boolean success) {
        this.success = success;
        return this;
    }

    public String getCode() {
        return code;
    }

    public Result<T> setCode(String code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public Result<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }
}